package com.zhong.gang.pda.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.databinding.DataBindingUtil;

import com.luck.picture.lib.tools.ToastUtils;
import com.zhong.gang.applib.CheseListener;
import com.zhong.gang.pda.R;
import com.zhong.gang.pda.api.Api;
import com.zhong.gang.pda.databinding.ItemShoufaBinding;
import com.zhong.gang.pda.db.user.UserBean;
import com.zhong.gang.pda.db.user.UserUtils;
import com.zhong.gang.applib.ui.chese.MaterialTypeBean;
import com.zhong.gang.applib.ui.chese.MaterialAct;
import com.zhong.gang.pda.ui.action.HuoBean;

import java.util.List;
import java.util.Locale;

public class ShouFaItemView extends FrameLayout implements CheseListener , TextWatcher {
    private ItemShoufaBinding binding;
    private ViewChangeListener listener;
    private boolean isShow;
    private String[] data = new String[]{"一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十"};
    private String id;
    private double price;
    private ShouFaListener shouFalistener;

    public double getPrice() {
        return price;
    }

    public void setListener(ViewChangeListener listener) {
        this.listener = listener;
    }

    public ShouFaItemView(Context context, boolean isShow,HuoBean.OrderMaterialTypeDetailBean bean,int number) {
        super(context);
        this.isShow = isShow;

        addView(bean,number);
    }

    public ShouFaItemView(Context context,boolean isShow,int number,ShouFaListener listener) {
        super(context);
        this.isShow = isShow;
        this.shouFalistener = listener;
        addView(number);
    }

    public String getItemId() {
        return id;
    }

    private void addView(HuoBean.OrderMaterialTypeDetailBean bean, int number){
        if (number >= data.length)
            return;
        this.id = bean.getMaterialTypeId()+"";
        if (bean.getMaterialType() != null && bean.getMaterialType().getMaterialTypePrice() != null)
            price = bean.getMaterialType().getMaterialTypePrice().getMaterialYardPrice();
        View view =LayoutInflater.from(getContext()).inflate(R.layout.item_shoufa,null);
        binding = DataBindingUtil.bind(view);
        binding.danjia.setVisibility(isShow?VISIBLE:GONE);
        binding.danjialine.setVisibility(isShow?VISIBLE:GONE);
        binding.setItem(this);
        binding.select.setTag(true);
        binding.typeName.setText(bean.getMaterialTypeName());
        binding.moneny.setText(bean.getPrice() <= 0?"":(bean.getPrice()+""));
        binding.weight.setText(bean.getWeight()<= 0?"":String.format(Locale.CHINA,"%.2f",(float)bean.getWeight()));
        binding.mTv.setText("装车料型"+data[number]);
        this.addView(view);
    }


    private void addView(int number){
        if (number >= data.length)
            return;
        View view =LayoutInflater.from(getContext()).inflate(R.layout.item_shoufa,null);
        binding = DataBindingUtil.bind(view);
        binding.weight.addTextChangedListener(this);
        binding.moneny.addTextChangedListener(this);
        binding.danjia.setVisibility(isShow?VISIBLE:GONE);
        binding.danjialine.setVisibility(isShow?VISIBLE:GONE);
        binding.setItem(this);
        binding.select.setTag(true);
        binding.weight.setEnabled(false);
        binding.selectLayout.setVisibility(VISIBLE);
        binding.type.setEnabled(true);
        binding.mTv.setText("装车料型"+data[number]);
        this.addView(view);
    }

    public ItemShoufaBinding getBinding() {
        return binding;
    }

    public ImageView getSelect(){
        if (binding == null)
            return new ImageView(getContext());
        return binding.select;
    }

    public View getSelectLayout(){
        if (binding == null)
            return new ImageView(getContext());
        return binding.selectLayout;
    }

    /**
     * 点击事件
     * @param view
     */
    public void onViewClicked(View view){
        int i = view.getId();
        if (i == R.id.type) {//点击事件
            UserBean userBean = UserUtils.getIntent(getContext()).getUser();
            MaterialAct.openAct(getContext(), Api.getMaterialType,
                    userBean.getMaterialYardId()+"",0+""
                    ,userBean.isLogin(),userBean.getToken(),userBean.getId()+"",userBean.getRoleId()+"",this);
        } else if (i == R.id.select_layout) {
            boolean nowStatus = (boolean) binding.select.getTag();
            if (listener != null)
                listener.onAddOrRemove(nowStatus);
        }
    }

    @Override
    public void onCheseBack(MaterialTypeBean bean) {
        this.id = bean.getId()+"";
        this.price = bean.getMaterialTypePrice().getMaterialYardPrice();
        binding.typeName.setText(bean.getName());
        if (bean.getMaterialTypePrice() != null){
            binding.moneny.setText(bean.getMaterialTypePrice().getMaterialYardPrice()+"");
        }else {
            binding.moneny.setText("");
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (shouFalistener != null)
            shouFalistener.onShowFa();
    }

    public interface ShouFaListener{
        void onShowFa();
    }
}
